package com.netease.nimlib.lucene.index;

import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field;
import org.apache.lucene.document.Field.Store;
import org.apache.lucene.document.FieldType;
import org.apache.lucene.document.LongField;
import org.apache.lucene.document.NumericDocValuesField;
import org.apache.lucene.document.SortedDocValuesField;
import org.apache.lucene.document.StoredField;
import org.apache.lucene.document.TextField;
import org.apache.lucene.index.IndexOptions;
import org.apache.lucene.search.ScoreDoc;
import org.apache.lucene.util.BytesRef;

import com.netease.nimlib.search.model.NIMIndexRecord;

public class MsgDocConverter {

	private static final FieldType FIELD_TYPE_INDEX_NOT_TOKENIZED;

	static {
		FIELD_TYPE_INDEX_NOT_TOKENIZED = new FieldType();
		FIELD_TYPE_INDEX_NOT_TOKENIZED.setIndexOptions(IndexOptions.DOCS);
		FIELD_TYPE_INDEX_NOT_TOKENIZED.setTokenized(false);
		FIELD_TYPE_INDEX_NOT_TOKENIZED.setStored(true);
	}

	public static final Document buildDoc(final NIMIndexRecord record) {
		Document doc = new Document();

		// type index
		Field type = new Field(MsgDocFieldName.TYPE, ((Long) record.type).toString(), FIELD_TYPE_INDEX_NOT_TOKENIZED);

		// subtype
		StoredField subtype = new StoredField(MsgDocFieldName.SUBTYPE, record.subtype);

		// dataid index
		Field dataId = new Field(MsgDocFieldName.DATAID, ((Long) record.dataid).toString(),
				FIELD_TYPE_INDEX_NOT_TOKENIZED);

		// id index sort
		Field id = new Field(MsgDocFieldName.ID, record.id, FIELD_TYPE_INDEX_NOT_TOKENIZED);
		SortedDocValuesField idSort = new SortedDocValuesField(MsgDocFieldName.ID, new BytesRef(record.id.getBytes()));

		// time index sort
		LongField time = new LongField(MsgDocFieldName.TIME, record.time, Store.YES);
		NumericDocValuesField timeSort = new NumericDocValuesField(MsgDocFieldName.TIME, record.time);

		// content index tokenized
		TextField content = new TextField(MsgDocFieldName.CONTENT, record.content, Store.YES);

		doc.add(type);
		doc.add(subtype);
		doc.add(dataId);
		doc.add(id);
		doc.add(idSort);
		doc.add(time);
		doc.add(timeSort);
		doc.add(content);

		return doc;
	}

	public static final NIMIndexRecord buildRecord(final Document doc, int count) {
		return buildRecord(doc, count, null);
	}

	public static final NIMIndexRecord buildRecord(final Document doc, final ScoreDoc scoreDoc) {
		return buildRecord(doc, 1, scoreDoc);
	}

	private static final NIMIndexRecord buildRecord(final Document doc, int count, final ScoreDoc scoreDoc) {
		NIMIndexRecord record = new NIMIndexRecord();
		record.type = Long.parseLong(doc.get(MsgDocFieldName.TYPE));
		record.subtype = Long.parseLong(doc.get(MsgDocFieldName.SUBTYPE));
		record.dataid = Long.parseLong(doc.get(MsgDocFieldName.DATAID));
		record.id = doc.get(MsgDocFieldName.ID);
		record.time = Long.parseLong(doc.get(MsgDocFieldName.TIME));
		record.content = doc.get(MsgDocFieldName.CONTENT);
		record.count = count;
		record.scoreDoc = scoreDoc;

		return record;
	}
}
